package dev.marsh.sniper.demo;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import java.util.List;

import dev.marsh.sniper.PickActivity;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends PickActivity implements EasyPermissions.PermissionCallbacks {
    private static final String TAG = "MainActivity";
    private static final int REQUEST_SNIPER_PERMISSIONS = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_sniper:
                ScanActivity.start(this);
                break;
            case R.id.pick:
                // 从相册选取图片，进行识别
                pick();
                break;
            case R.id.full:
                FullActivity.start(this);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        requestSniperPermissions();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    }

    @AfterPermissionGranted(REQUEST_SNIPER_PERMISSIONS)
    private void requestSniperPermissions() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, getString(R.string.request_permission), REQUEST_SNIPER_PERMISSIONS, perms);
        }
    }

    @Override
    protected void result(boolean success) {
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "Result is " + success);
        }
    }
}
